home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / compare.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  4KB  |  122 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* compare.c */
  25.  
  26. #include "builtin.h"
  27.  
  28. extern float floatval();
  29.  
  30. b_COMPARE()
  31. {
  32.    /* reg1, reg2: two terms to be compared; reg3: free var to set the
  33.     * result of the comparison: reg1 < reg2: int<0,  reg1 = reg2: 0, >0 o/w.
  34.     * reg3 must be free and a LOCAL new variable
  35.     * LIST is the largest term of the all four kinds
  36.     */
  37.  
  38.    if (!unify(reg[3], MAKEINT(compare(reg[1], reg[2]))))
  39.       {FAIL0;}
  40. }
  41.  
  42. compare(val1, val2)
  43. LONG val1, val2;
  44. {
  45.    register LONG_PTR top;
  46.    WORD              a, b;
  47.    LONG              c;
  48.    PSC_REC_PTR       psc_ptr1, psc_ptr2;
  49.    float             fpt_val;
  50.  
  51.    DEREF(val2);
  52. cd:
  53.    switch (TAG(val1)) {
  54.       case FREE: NDEREF(val1, cd);
  55.                  if (ISVAR(val2))
  56.              return (val1 - val2);
  57.                  else return -1;
  58.       case NUM : if (ISVAR(val2))
  59.              return 1;
  60.                  else if (ISINTEGER(val1) && ISINTEGER(val2))
  61.                     return (INTVAL(val1) - INTVAL(val2));
  62.                  else if (ISFLOAT(val1) && ISFLOAT(val2)) {
  63.                     fpt_val = floatval(val1) - floatval(val2);
  64.                     if (fpt_val > 0)
  65.                 return 1;
  66.                     else if (fpt_val == 0)
  67.                 return 0;
  68.                     else return -1;
  69.                  } else return -1;
  70.       case CS  : if (ISVAR(val2) || ISINTEGER(val2))
  71.              return 1;
  72.                  else if (ISLIST(val2))
  73.              return -1;
  74.                  else {
  75.                     psc_ptr1 = GET_STR_PSC(val1);
  76.                     psc_ptr2 = GET_STR_PSC(val2);
  77.                     a = GET_ARITY(psc_ptr1);
  78.                     b = GET_ARITY(psc_ptr2);
  79.                     if (a != b)
  80.                 return (a - b);
  81.                     c = comalpha(psc_ptr1, psc_ptr2);
  82.                     if (c || a == 0)
  83.                 return c;
  84.                     UNTAG(val1);
  85.                     UNTAG(val2);
  86.                     for (b = 1; b <= a; b++) {
  87.                        c = compare(FOLLOW(((LONG_PTR)val1)+b),
  88.                                    FOLLOW(((LONG_PTR)val2)+b));
  89.                        if (c)
  90.                    break;
  91.                     }
  92.                     return c;
  93.                  }
  94.                  /* break; */
  95.       case LIST: if (!ISLIST(val2))
  96.              return 1;
  97.                  else {
  98.                     UNTAG(val1);
  99.                     UNTAG(val2);
  100.                     c = compare(FOLLOW(val1), FOLLOW(val2));
  101.                     if (c)
  102.                        return c;
  103.                     else
  104.                        return compare(FOLLOW(((LONG_PTR)val1)+1),
  105.                                       FOLLOW(((LONG_PTR)val2)+1));
  106.                 }
  107.                 /* break; */
  108.    }  /* switch */
  109. }  /* compare */
  110.  
  111. comalpha(psc_ptr1, psc_ptr2)
  112. PSC_REC_PTR psc_ptr1, psc_ptr2;
  113. {
  114.    CHAR name1[256], name2[256];
  115.  
  116.    if (psc_ptr1 == psc_ptr2)
  117.       return 0;
  118.    namestring(psc_ptr1, name1);
  119.    namestring(psc_ptr2, name2);
  120.    return strcmp(name1, name2);
  121. }
  122.